package com.llxz.blog.acl.controller;

import com.llxz.blog.acl.entity.Permission;
import com.llxz.blog.acl.service.PermissionService;
import com.llxz.blog.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author xzs
 * @description: date: 2021/9/4 9:35
 */
@RestController
@RequestMapping("/acl/permission")
@CrossOrigin
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    //获取全部菜单
    @GetMapping("all")
    public R all() {
        List<Permission> list =  permissionService.queryAllMenu();
        return R.ok().data("children",list);
    }

    //递归删除
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        permissionService.removeChildById(id);
        return R.ok();
    }

    //为角色分配权限
    @PostMapping("Assign")
    public R assign(@RequestParam String roleId,@RequestParam String[] permissionId) {
        permissionService.saveRolePermission(roleId,permissionId);
        return R.ok();
    }

    @GetMapping("getByRoleId/{roleId}")
    public R getById(@PathVariable String roleId) {
        List<Permission> list = permissionService.selectAllMenu(roleId);
        return R.ok().data("children", list);
    }

    @PostMapping("add")
    public R add(@RequestBody Permission permission){
        permissionService.save(permission);
        return R.ok();
    }

    @PutMapping("update")
    public R update(@RequestBody Permission permission){
        permissionService.updateById(permission);
        return R.ok();
    }

}
